package cn.wonderland.org.controller;

import cn.wonderland.basic.anno.LogAnnotation;
import cn.wonderland.basic.anno.PreAuthorize;
import cn.wonderland.basic.jwt.UserInfo;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.LoginContext;
import cn.wonderland.basic.util.PageList;
import cn.wonderland.org.domain.Department;
import cn.wonderland.org.query.DepartmentQuery;
import cn.wonderland.org.service.DepartmentService;
import cn.wonderland.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    /*
     * 查询所有信息
     * */
    @GetMapping()
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /*
     * 通过id查询单个信息
     * */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public Department findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /*
     * 通过id删除单个信息
     * */
    @DeleteMapping("/{id}")
    @LogAnnotation(module="删除",operator="删除部门信息")
    @PreAuthorize(name="部门批量删除",sn = "department:batchDelete")
    public AjaxResult deleteById(@PathVariable("id") Long id) {

        // 调用删除方法
        departmentService.delete(id);
        // 返回成功的AjaxResult
        return AjaxResult.me().success();

    }

    /*
     * 新增或者修改信息 put
     * */
    @PutMapping()
    // 此注解表示对该接口进行日志记录
    @LogAnnotation(module="部门",operator="部门列表新增修改")
    public AjaxResult addOrUpdate(@RequestBody Department department) {

        if (department.getId() == null) { // 如果id为空，那么执行新增方法
            departmentService.add(department);
        } else { // 否则执行修改方法
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    /*
     * 分页查询
     * */
    @PostMapping()
    // 此注解表示对该接口进行日志记录
//    @LogAnnotation(module="部门",operator="获取部门分页列表")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query) throws Exception {
        Logininfo logininfo = LoginContext.getLogininfo();
        System.out.println(logininfo);
        return departmentService.queryByPage(query);
    }

    /*
     * 批量删除信息
     * */
    @PatchMapping()
    // 此注解表示对该接口进行日志记录
    @LogAnnotation(module="部门",operator="批量删除部门列表")
    @PreAuthorize(name="部门批量删除",sn = "department:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }


    // 无限级数
    @GetMapping("/tree")
    @PreAuthorize(name = "部门无限级数", sn = "department:tree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }

}
